#if !defined(_ASTAGE_H_)
#define _ASTAGE_H_

#include "AObject.h"
#include "AAnimateObject.h"
#include "AStageLayer.h"
#include "AEvent.h"
#include "AAudio.h"

class CAXMLNode;

typedef enum enumStageLoadState
{
	SLS_Initialize,
	SLS_LoadModels,
	SLS_LoadLayers0,
	SLS_LoadLayers,
	SLS_Finished,
}
EStageLoadState;

class CAStage : public CCLayer, public CATimeProvider, public CAICoordinateConverter
{
private:
	unsigned int _frames;
	ccTime _fpsTimer;
	float _fps;

	virtual void update(ccTime dt);

protected:
	CAString _name;

	CAStringMap<CAStageLayer>* _sublayers;
	CAStageLayer* _getSubLayer(const CAString& name)
	{
		return (CAStageLayer*)_sublayers->objectForKey(name);
	}

	CAStringMap<CCSpriteBatchNode>* _pRenderGroup;

	int _createLayer(CAXMLNode* pnode);
	int _loadSpriteModel(CAXMLNode* pnode);
	int _loadMusic(CAXMLNode* pnode);
	int _loadSound(CAXMLNode* pnode);

	CAAnimated2Points _animOffset;
	CCPoint _posOffset;

	EStageLoadState _loadState;
	CAXMLNode* _pxml;
	unsigned int _objectsLoaded;
	unsigned int _objectsCount;
	float _percentLoaded;
	virtual unsigned int _getUnloadedResourcesCount();
	virtual unsigned int _loadUnknownFormatObject() { return 0; };

	ETouchState		_touchstate;
	CCArray* _aryFocusedLayer;
	void _dipatchEvent(ETouchState ts, CCTouch* pTouch);

	CAAudio* _paudio;
public:
	CAStage(const char* name);
	virtual ~CAStage(void);

	const CAString& name() const { return _name; }

	EStageLoadState loadProgressively();
	float getLoadPercent() { return _percentLoaded; }

	float fps() const { return _fps; }
	//attrs
	void setOffset(const CCPoint& pt, float intime = 0.5f);
	const CCPoint& offset() const { return _posOffset; }
	//const CCPoint& speed() const { return _speed; }
	virtual void game2pixel(CCPoint& pos, bool bFollowCamera) const 
	{
		if (bFollowCamera)
		{
			pos.x -= _posOffset.x;
			pos.y -= _posOffset.y;
		}
	};
	virtual void pixel2game(CCPoint& pos, bool bFollowCamera) const 
	{
		if (bFollowCamera)
		{
			pos.x += _posOffset.x;
			pos.y += _posOffset.y;
		}
	};
	CCPoint getPointFromPercent(const CCPoint& pos, bool bFollowCamera);

	CCSpriteBatchNode* getSpriteGroup(const CAString& name);

	virtual void onEnter();
	virtual void onUpdate();
	virtual void onExit();

	virtual void onRestart();

	void setFocus(CAStageLayer* pLayer, bool push = false);
	CAStageLayer* getFocus();
	//void killFocus(CAStageLayer* pLayer);

	virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
	virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
	virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);

 	//virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
 	//virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
 	//virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);

	virtual void onSystemEvent(ESystemEvent evt, void* data = null);

	void playMusic(const char* psz);
	void playEffect(const char* psz, float volume = 1.0f);
	bool isMusicMute() const;
	bool isSoundMute() const;
	void enableMusic(bool e);
	void enableSound(bool e);
};

#endif //_ASTAGE_H_